﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Janksen.ValidationHelper
{
	public class ValidationHelper<T>
	{
		#region 成员
		private T m_ValidationValue;
		private string m_ValidationName;
		private List<string> m_FailedValidateMessage;
		private bool m_IsPassValition;
        private bool m_IsThrowException;
		#endregion

		#region 属性
		/// <summary>
		/// 获取待验证的参数的值.
		/// </summary>
		public T ValidationValue
		{
			get
			{
				return m_ValidationValue;
			}
		}

		/// <summary>
		/// 获取待验证的参数的名称.
		/// </summary>
		public string ValidationName
		{
			get
			{
				return m_ValidationName;
			}
		}

		/// <summary>
		/// 验证不通过的提示信息
		/// </summary>
		public List<string> FailedValidateMessage
		{
			get
			{
				return m_FailedValidateMessage;
			}
		}

		/// <summary>
		/// 是否通过验证
		/// </summary>
		public bool IsPassValidation
		{
			get
			{
				return m_IsPassValition;
			}
		}


        /// <summary>
        /// 验证不通过时是否抛出异常
        /// </summary>
        public bool IsThrowException
        {
            get
            {
                return m_IsThrowException;
            }
        }
				
		#endregion

		#region 构造函数
        /// <summary>
        /// 创建一个ValidationHelper的对象
        /// </summary>
        /// <param name="value">待验证的参数的值</param>
        /// <param name="name">待验证的参数的名称</param>
        /// <param name="isThrowException">验证不通过时是否抛出异常</param>
		public ValidationHelper(T value , string name,bool isThrowException)
		{
			m_ValidationValue = value;
			m_ValidationName = name;
            m_IsThrowException = isThrowException;
			m_IsPassValition=true;
			m_FailedValidateMessage=new List<string>();

			//每个验证的都不能为Null
			NotNull();
		}
		#endregion

		#region 基本方法
		/// <summary>
		/// 验证参数不为其默认值不为Null
		/// </summary>
		/// <returns>this指针以方便链式调用.</returns>
		private ValidationHelper<T> NotNull()
		{
			if (m_ValidationValue==null)
			{
				FailedValidate(String.Format("参数{0}不能为null" , m_ValidationName));
			}
			return this;
		}

		/// <summary>
		/// 验证参数不为其默认值
		/// </summary>
		/// <returns>this指针以方便链式调用.</returns>
		public ValidationHelper<T> NotDefault()
		{
			if (m_ValidationValue.Equals(default(T)))
			{
				if (m_ValidationValue is ValueType)
				{
					FailedValidate(String.Format("参数{0}不能使用默认值" , m_ValidationName));
				}
				else
				{
					FailedValidate(String.Format("参数{0}不能为null" , m_ValidationName));
				}
			}
			return this;
		}


        /// <summary>
        /// 验证是否制定的类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public ValidationHelper<T> IsTypeOf(Type type)
        {
            if (m_ValidationValue.GetType() != type)
            {
                FailedValidate(String.Format("参数{0}的类型不是{1}", m_ValidationName,type.ToString()));
            }
            return this;
        }

		///// <summary>   
		/////  使用自定义方法进行验证.  
		///// </summary> 
		///// <param name="rule">用以验证的自定义方法.</param> 
		///// <returns>this指针以方便链式调用.</returns>   
		//public ValidationHelper<T> CustomValidation(Action<T , string> rule)
		//{
		//    rule(m_ValidationValue , m_ValidationName);
		//    return this;
		//}



        /// <summary>
        /// 验证失败
        /// </summary>
        /// <param name="failedValidateMessage"></param>
		internal void FailedValidate(string failedValidateMessage)
		{
			this.m_FailedValidateMessage.Add(failedValidateMessage); //增加验证失败信息
			this.m_IsPassValition=false; //验证失败
            
            if (m_IsThrowException == true)
            {
                throw new Exception(failedValidateMessage);
            }
		}			

		#endregion

	}
}
